package com.deluxelx.soul.inn.web.workbench;

import cn.hutool.core.collection.CollectionUtil;
import com.deluxelx.soul.inn.bean.article.ArticleDTO;
import com.deluxelx.soul.inn.bean.article.ArticleVO;
import com.deluxelx.soul.inn.bean.course.CourseDTO;
import com.deluxelx.soul.inn.bean.course.CourseVO;
import com.deluxelx.soul.inn.common.result.SoulInnResult;
import com.deluxelx.soul.inn.service.article.ArticleService;
import com.deluxelx.soul.inn.service.course.CourseService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 咨询师工作台-Controller类
 *
 * @author liaozihao
 * @date 2024-04-24 14:42
 */
@RestController
@RequestMapping("/soul/inn/workbench")
public class WorkbenchController {

    @Resource
    private ArticleService articleService;

    @Resource
    private CourseService courseService;

    @GetMapping("/myArticleList")
    public SoulInnResult<List<ArticleVO>> getMyArticleList(String counselorId) {
        List<ArticleVO> articleVOS = articleService.fetchArticleListByCounselor(counselorId);
        if (CollectionUtil.isNotEmpty(articleVOS)) {
            return SoulInnResult.success(articleVOS);
        }
        return SoulInnResult.fail("获取工作台我的文章失败");
    }

    @PostMapping("/saveArticle")
    public SoulInnResult<String> saveArticle(@RequestBody ArticleDTO param) {
        String articleId = articleService.saveArticle(param);
        if (articleId != null) {
            return SoulInnResult.success(articleId);
        }
        return SoulInnResult.fail("保存文章失败");
    }

    @PostMapping("/modifyArticle")
    public SoulInnResult<Boolean> modifyArticle(@RequestBody ArticleDTO param) {
        try {
            return SoulInnResult.success(articleService.modifyArticle(param));
        } catch (Exception e) {
            return SoulInnResult.fail("修改文章失败");
        }
    }

    @PostMapping("/removeArticle")
    public SoulInnResult<Boolean> removeArticle(String articleId) {
        try {
            return SoulInnResult.success(articleService.removeArticle(articleId));
        } catch (Exception e) {
            return SoulInnResult.fail("删除文章失败");
        }
    }

    @GetMapping("/myCourseList")
    public SoulInnResult<List<CourseVO>> getMyCourseList(String counselorId) {
        List<CourseVO> courseVOS = courseService.fetchCourseListByCounselor(counselorId);
        if (CollectionUtil.isNotEmpty(courseVOS)) {
            return SoulInnResult.success(courseVOS);
        }
        return SoulInnResult.fail("获取工作台我的课程失败");
    }

    @PostMapping("/saveCourse")
    public SoulInnResult<String> saveCourse(@RequestBody CourseDTO param) {
        String courseId = courseService.saveCourse(param);
        if (courseId != null) {
            return SoulInnResult.success(courseId);
        }
        return SoulInnResult.fail("保存课程失败");
    }

    @PostMapping("/modifyCourse")
    public SoulInnResult<Boolean> modifyCourse(@RequestBody CourseDTO param) {
        try {
            return SoulInnResult.success(courseService.modifyCourse(param));
        } catch (Exception e) {
            return SoulInnResult.fail("修改课程失败");
        }
    }

    @PostMapping("/removeCourse")
    public SoulInnResult<Boolean> removeCourse(String courseId) {
        try {
            return SoulInnResult.success(courseService.removeCourse(courseId));
        } catch (Exception e) {
            return SoulInnResult.fail("删除课程失败");
        }
    }
}
